{include file="public/layout" /}
<body>
  <div class="layui-fluid">
      <div class="layui-row layui-col-space15">
          <div class="layui-col-md12">
            <div class="layui-card">
              <div class="layui-card-body" id="postForm" pad15>
                <div class="layui-form" wid100 lay-filter="">
                  <div class="layui-form-item">
                    <label class="layui-form-label"><b>*</b>用&nbsp;&nbsp;户&nbsp;&nbsp;名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="user_name" value="" id="user_name" lay-verify="required" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label"><b>*</b>登录密码</label>
                    <div class="layui-input-inline">
                     <input type="password" name="password" id="password" value="" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label"><b>*</b>确认密码</label>
                    <div class="layui-input-inline">
                     <input type="password" name="password2" id="password2" lay-verify="required|check_password2" value="" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">笔名</label>
                    <div class="layui-input-inline">
                      <input type="text" name="pen_name" value="" class="layui-input">
                    </div>
                    <div class="layui-input-inline layui-btn-container " style="width: auto;">
                      <div class="layui-form-mid layui-word-aux ey_helptips"></div>
                      <div class="layui-form-inline2 ey_helptips_txt">发布文档后显示责任编辑的名字</div>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">真实姓名</label>
                    <div class="layui-input-inline">
                      <input type="text" name="true_name" value="" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">手机号码</label>
                    <div class="layui-input-inline">
                      <input type="text" name="mobile" value="" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label"><strong>管理员权限设置</strong></label>
                    <div class="layui-input-inline"></div>
                  </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">管理员权限组</label>
                    <div class="layui-input-inline">
                      <input type="radio" name="role_id" value="-1" lay-filter="role_id" title="超级管理员" checked><br>
                      {foreach name="admin_role_list" item="role" key="k"}
                      <input type="radio" name="role_id" value="{$role.id}" lay-filter="role_id" title="{$role.name}" {if condition="$role.id == $Request.param.role_id"} checked{/if}><br>
                      {/foreach}
                      <!--<p id="custom_role" style="padding-left: 30px; text-decoration:underline;"><label><a href="javascript:void(0);" data-url="{:url('AuthRole/add', array('iframe'=>1))}" id="authRole_add">自定义</a></label></p>-->
                    </div>
                  </div>
<!--                   <div class="layui-form-item">
                    <label class="layui-form-label"><strong>当前权限组预览</strong></label>
                    <div class="layui-input-inline"></div>
                  </div> -->
                  <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="hidden" name="admin_id" value="{$info.admin_id}">
                        <button class="layui-btn" lay-submit lay-filter="formSubmit">确认提交</button>
                    </div>
                  </div>
              </div>
            </div>
          </div>
        </div>
  </div>
  <textarea name="admin_role_list" id="admin_role_list" class="none">{$admin_role_list|json_encode}</textarea>

  <script>
  layui.config({
    base: '__SKIN__/' //静态资源所在路径
    ,version: '{$version}'
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'set'], function(){
    var $ = layui.$
    ,layer = layui.layer
    ,form = layui.form;
  
    // 默认全部禁用复选框
    $('#postForm input[type="checkbox"]').attr("disabled","disabled");

    /*超级管理员默认全选复选框*/
    if (-1 == $('#postForm input[name="role_id"]').val()) {
        $('#postForm input[type="checkbox"]').attr('checked', 'checked');
        $('#postForm img.cboximg').attr('src', '__SKIN__/images/ok.png');
    }
    /*--end*/

    form.render();

    form.verify({
      check_password2: function(value, item){ //value：表单的值、item：表单的DOM对象
        var password = $('input[name=password]').val();
        var password2 = $('input[name=password2]').val();
        if (password != '' || password2 != '') {
            if (password != password2) {
                return '两次密码输入不一致！';
            }
        }
      }
    }); 

    $('.arctype_bg').bind('click', function(){
        var acid = $(this).next().find('input').val(), input = 'arctype_child_' + acid;
        $('.arctype_child').hide();
        if( $(this).attr('class').indexOf('expandable') == -1 ){
            $(this).removeClass('collapsable').addClass('expandable');
        }else{
            $('.arctype_bg').removeClass('collapsable').addClass('expandable');
            $(this).removeClass('expandable').addClass('collapsable');
            $('#'+input).show();
        }
    });
    $('.arctype_cbox').bind('click', function(){
        var acid = $(this).val(), input = 'arctype_child_' + acid;
        var pid = $(this).data('pid');
        var tpid = $(this).data('tpid');
        if($(this).attr('checked')){
            if (0 < $('input[data-pid="'+pid+'"]:checked').length) {
                $('.arctype_id_'+pid).attr('checked', 'checked');
                $('.arctype_id_'+pid).parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
            }
            if (0 < $('#arctype_child_'+tpid).find('input[type="checkbox"]:checked').length) {
                $('.arctype_id_'+tpid).attr('checked', 'checked');
                $('.arctype_id_'+tpid).parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
            }
            $('#'+input).find('input[type="checkbox"]').attr('checked', 'checked');
            $('#'+input).find('input[type="checkbox"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
        }else{
            if (1 > $('input[data-pid="'+pid+'"]:checked').length) {
                $('.arctype_id_'+pid).removeAttr('checked');
                $('.arctype_id_'+pid).parent().find('img.cboximg').attr('src', '__SKIN__/images/del.png');
            }
            if (1 > $('#arctype_child_'+tpid).find('input[type="checkbox"]:checked').length) {
                $('.arctype_id_'+tpid).removeAttr('checked');
                $('.arctype_id_'+tpid).parent().find('img.cboximg').attr('src', '__SKIN__/images/del.png');
            }
            $('#'+input).find('input[type="checkbox"]').removeAttr('checked');
            $('#'+input).find('input[type="checkbox"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/del.png');
        }
    });
    $('#select_cud').bind('click', function(){
        if($(this).attr('checked')){
            $('#postForm input[name^="cud"]').attr('checked', 'checked');
            $('#postForm input[name^="cud"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
        }else{
            $('#postForm input[name^="cud"]').removeAttr('checked');
            $('#postForm input[name^="cud"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/del.png');
        }
    });

    $('#select_all_permission').bind('click', function(){
        if($(this).attr('checked')){
            $('#postForm input[name^="permission"]').attr('checked', 'checked');
            $('#postForm input[name^="permission"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
        }else{
            $('#postForm input[name^="permission"]').removeAttr('checked');
            $('#postForm input[name^="permission"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/del.png');
        }
    });
    $('#postForm input[name^="permission"],#postForm input[name^="cud"]').bind('click', function(){
        hasSelectAll();
    });

    function hasSelectAll(){
        var c = true;
        $('#postForm input[name^="permission"]').each(function(idx, ele){
            if(! $(ele).attr('checked')){
                c = false;
                return;
            }
        });
        if(c){
            $('#select_all_permission').attr('checked', 'checked');
            $('#select_all_permission').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
        }else{
            $('#select_all_permission').removeAttr('checked');
            $('#select_all_permission').parent().find('img.cboximg').attr('src', '__SKIN__/images/del.png');
        }

        var c1 = true;
        $('#postForm input[name^="cud"]').each(function(idx, ele){
            if(! $(ele).attr('checked')){
                c1 = false;
                return;
            }
        });
        if(c1){
            $('#select_cud').attr('checked', 'checked');
            $('#select_cud').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
        }else{
            $('#select_cud').removeAttr('checked');
            $('#select_cud').parent().find('img.cboximg').attr('src', '__SKIN__/images/del.png');
        }
    }

    function changeRole(value){
        if (-1 == value) {
            $('#postForm input[type="checkbox"]').attr("checked","checked").attr('disabled', 'disabled');
            $('#postForm img.cboximg').attr('src', '__SKIN__/images/ok.png');
            return;
        }
        
        $('#postForm input[name!="role_id"]').removeAttr('checked').removeAttr('disabled');
        $('#postForm img.cboximg').attr('src', '__SKIN__/images/del.png');

        /*自定义权限组*/
        // if(value == "0"){
        //     $('#postForm input[name!="role_id"]').attr('checked', 'checked');
        //     $('#postForm input[name="online_update"]').removeAttr('checked');
        //     $('#postForm input[name="only_oneself"]').removeAttr('checked');
        //     return ;
        // }
        /*--end*/
        var admin_role_list = JSON.parse($('#admin_role_list').val());
        for(var i in admin_role_list){
            var item = admin_role_list[i];
            if(item.id == value){
                if(item.online_update){
                    $('#postForm input[name="online_update"]').attr('checked', 'checked');
                    $('#postForm input[name="online_update"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
                };
                // if(item.editor_visual){
                //     $('#postForm input[name="editor_visual"]').attr('checked', 'checked');
                //     $('#postForm input[name="editor_visual"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
                // };
                if(item.only_oneself){
                    $('#postForm input[name="only_oneself"]').attr('checked', 'checked');
                    $('#postForm input[name="only_oneself"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
                };
                if(item.cud){
                    item.cud.map(function(row){
                        $('#postForm input[name^="cud"][value="'+row+'"]').attr('checked', 'checked');
                        $('#postForm input[name^="cud"][value="'+row+'"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
                    });
                }

                if(item.permission){
                    for(var p in item.permission){
                        if(p == 'plugins'){
                            if(item.permission[p]){
                                for(var pluginId in item.permission[p]){
                                    $('#postForm input[name="permission['+p+']['+pluginId+'][code]"][value="'+pluginId+'"]').attr('checked', 'checked');
                                    $('#postForm input[name="permission['+p+']['+pluginId+'][code]"][value="'+pluginId+'"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
                                    if(item.permission[p][pluginId].child){
                                        item.permission[p][pluginId].child.map(function(row){
                                            $('#postForm input[name="permission['+p+']['+pluginId+'][child][]"][value="'+row+'"]').attr('checked', 'checked');
                                            $('#postForm input[name="permission['+p+']['+pluginId+'][child][]"][value="'+row+'"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
                                        });
                                    }
                                }
                            }
                        }else{
                            item.permission[p].map(function(row){
                                $('#postForm input[name="permission['+p+'][]"][value="'+row+'"]').attr('checked', 'checked');
                                $('#postForm input[name="permission['+p+'][]"][value="'+row+'"]').parent().find('img.cboximg').attr('src', '__SKIN__/images/ok.png');
                            });
                        }
                        
                    }
                }

                hasSelectAll();
                $('#postForm input[type="checkbox"]').attr('disabled', 'disabled');
                break;
            }
        }
    }

    function addRole(obj)
    {
        // iframe窗
        var iframes = layer.open({
            type: 2,
            title: '自定义权限组',
            fixed: true, //不固定
            shadeClose: false,
            shade: 0.3,
            maxmin: false, //开启最大化最小化按钮
            // area: ['90%', '90%'],
            content: $(obj).data('url')
        });
        layer.full(iframes);
    }

    function custom_role(str, new_role_id, auth_role_list)
    {
        $('#custom_role').before(str);
        $('#admin_role_list').val(auth_role_list);
        changeRole(new_role_id);
    }

    // 监听单选项
    form.on('radio(role_id)', function(data){
      changeRole(data.value);
    });

    $('#authRole_add').click(function(){
        addRole(this);
    });

    //监听提交
    form.on('submit(formSubmit)', function(data){
        var load = layer_loading();
        data.field._ajax = 1;
        $.ajax({
            type : 'post',
            url : "{:url('Admin/admin_add')}",
            data : data.field,
            dataType : 'json',
            success : function(res){
                layer.close(load); //关闭loading
                if(res.code == 1){
                    layer.msg(res.msg, {icon: 1, time: 1000}, function(){
                        parent.gourl(res.url);
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.close(load); //关闭loading
                showErrorAlert();
            }
        });
        return false;
    });

  });

  </script>
{include file="public/footer" /}